package com.iwop.moto.service.impl;

import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.BaseService;
import com.iwop.moto.service.MGoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Created by liuwb on 2016-07-25.
 */
@SuppressWarnings("serial")
@Service("mgoodsService")
public class MGoodsServiceImpl extends BaseService implements MGoodsService {

    /**
     * liuwb 2016-07-25 16:44
     * 2016-07-25 新增商品
     * @param SRModel 参数组
     */
    public void insertSelective (SuperRecordModel SRModel) {
        getSqlSession().insert("MGoodsMapper.insertSelective",SRModel);
    }

    /**
     * 功能:新增商品明细（规格）
     */
    public void insertDetailSelective (SuperRecordModel SRModel) {
        getSqlSession().insert("MGoodsMapper.insertDetailSelective",SRModel);
    }

    /**
     * 功能:根据状态获取 销售中/审核中/审批未通过的商品列表
     * 参数:storeId 店铺id
     * 参数:status 状态 1销售中 0审核中 2未通过
     */
    public List<SuperRecordModel> findStoreByIdAndStatus(SuperRecordModel SRModel){
        return getSqlSession().selectList("MGoodsMapper.findStoreByIdAndStatus",SRModel);
    }

    /**
     * 功能:商品 - 详细信息 - 主表
     * 参数: sid 主键
     */
    public SuperRecordModel findFgoodsById(SuperRecordModel SRModel){
        return getSqlSession().selectOne("MGoodsMapper.findFgoodsById", SRModel);
    }

    /**
     * 功能:商品 - 详细信息 - 商品规格明细
     * 参数: sid 主键
     */
    public List<SuperRecordModel> findFgoodsListDetail(SuperRecordModel SRModel){
        return getSqlSession().selectList("MGoodsMapper.findFgoodsListDetail",SRModel);
    }

    /**
     * 功能:商品 - 详细信息
     * 参数: sid 主键
     */
    public SuperRecordModel findFgoodsDetailById(SuperRecordModel SRModel){
        return getSqlSession().selectOne("MGoodsMapper.findFgoodsDetailById", SRModel);
    }

    /**
     * 功能:商品 - 修改库存
     * 参数:sid 商品规格信息主键
     * 参数:stocknums 修改的库存数
     */
    public void upMGoodsDetailStocknums(SuperRecordModel SRModel){
        getSqlSession().update("MGoodsMapper.upMGoodsDetailStocknums",SRModel);
    }

    /**
     * 功能: 商品修改审批状态
     * 参数: sid 商品主键
     * 参数: status [0:审核中][1:通过][2:拒绝][3:取消发布]
     * 参数: checkuserid 审批人id
     */
    public void upMGoodsCheckstatus(SuperRecordModel SRModel){
        getSqlSession().update("MGoodsMapper.upMGoodsCheckstatus",SRModel);
    }


    /**
     * 功能: 商品修改上架状态
     * 参数: sid 商品主键
     * 参数: status [0:未发布][1:上架][2:下架]
     */
    public void upMGoodsStatusFlag(SuperRecordModel SRModel){
        getSqlSession().update("MGoodsMapper.upMGoodsStatusFlag",SRModel);
    }

    public void upMGoodsDetailStatusFlag(SuperRecordModel SRModel){
        getSqlSession().update("MGoodsMapper.upMGoodsDetailStatus",SRModel);
    }

    /**
     * 功能:获取审批状态
     * 参数:sid
     */
    public String getCheckStatusBySid(SuperRecordModel SRModel){
        return getSqlSession().selectOne("MGoodsMapper.getCheckStatusBySid",SRModel);
    }

    /**
     * 功能:更新商品发货地，商品描述
     * 参数:sid 商品主键
     * 参数:describe 商品描述
     * 参数:address 商品归属地
     */
    @Override
    public void upMGoodsAddressDescribe(SuperRecordModel SRModel) {
        getSqlSession().update("MGoodsMapper.upMGoodsAddressDescribe",SRModel);
    }

    /**
     * 功能：获取商品详情上架的列表，通过商品详情Id
     * */
    @Override
    public List getMGoodsByDetailId(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MGoodsMapper.selectMGoodsByDetailId",SRModel);
    }

    @Override
    public boolean findFGoodsdetailIsOut(String mGoodsId) {
        SuperRecordModel srm = getSqlSession().selectOne("MGoodsMapper.selectFGoodsdetailIsOut", mGoodsId);
        Long outGoods = (Long)srm.get("outGoods");
        Long allCount = (Long)srm.get("allCount");
        return outGoods==allCount;
    }

    @Override
    public void upMGoodsStocknums(SuperRecordModel SRModel) {
        getSqlSession().update("MGoodsMapper.upMGoodsStocknums", SRModel);
    }

    @Override
    public void upMgoodsDeleteFlag(SuperRecordModel SRModel) {
        getSqlSession().update("MGoodsMapper.upMgoodsDeleteFlag", SRModel);
        getSqlSession().update("MGoodsMapper.upMgoodsDetailDeleteFlag", SRModel);
    }

    public void upMgoodsDetailDelFlag(SuperRecordModel SRModel){
        getSqlSession().update("MGoodsMapper.upMgoodsDetailDelFlag", SRModel);
    }

    @Override
    public List<SuperRecordModel> findFgoodsDetailByGoodsId(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MGoodsMapper.findFgoodsDetailByGoodsId", SRModel);
    }

    @Override
    public List<SuperRecordModel> findMGoodsListToAudit(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MGoodsMapper.findMGoodsListToAudit", SRModel);
    }

    @Override
    public int countMGoodsToCheck(SuperRecordModel SRModel) {
        return getSqlSession().selectOne("MGoodsMapper.countMGoodsToCheck", SRModel);
    }

    @Override
    public void deleteMGoods(String userId) {
        getSqlSession().delete("MGoodsMapper.deleteMGoods", userId);
    }

    @Override
    public void deleteMGoodsDetail(String userId) {
        getSqlSession().delete("MGoodsMapper.deleteMGoodsDetail", userId);
    }
}